package com.woniuxy.hrp.web;

import com.woniuxy.hrp.entity.ArrangeTime;
import com.woniuxy.hrp.entity.Doctor;
import com.woniuxy.hrp.entity.DoctorAndTime;
import com.woniuxy.hrp.entity.Reservation;
import com.woniuxy.hrp.service.ArrangeTimeService;
import com.woniuxy.hrp.service.DoctorAndTimeService;
import com.woniuxy.hrp.service.DoctorService;
import com.woniuxy.hrp.service.ReservationService;
import com.woniuxy.hrp.unityhandler.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Description：
 * @Author：曹杰克
 * @CreateTime：2022/8/30 14:47
 */
@CrossOrigin
@Transactional
@RestController
@RequestMapping("/api/v1/reservation")
public class ReservationController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private DoctorAndTimeService doctorAndTimeService;
    @Autowired
    private ArrangeTimeService arrangeTimeService;
    @Autowired
    private DoctorService doctorService;

    /**
     * 取消预约
     * @param id
     * @return
     */
    @RequestMapping("/deleteReservation")
    public ResponseEntity deleteReservationById(int id) {
        reservationService.deleteReservationById(id);
        return ResponseEntity.SUCCESS;
    }

    @CrossOrigin
    @PostMapping("/reservation/selecon")
    public ResponseEntity<List<Reservation>> seleByCon(@RequestBody Reservation reservation) {
        /**
         * @Author 李国杰
         * @Description //分条件查预约信息
         * @Date 10:11 2022/8/31
         * @Param [doctor]
         * @return com.woniuxy.hrp.unityhandler.ResponseEntity<java.util.List < com.woniuxy.hrp.entity.Doctor>>
         *
         **/
        System.out.println(reservation);
        return new ResponseEntity("200", "success", reservationService.seleReservationByCon(reservation));
    }

    @CrossOrigin
    @PostMapping("/reservation/sta")
    public ResponseEntity<List<Reservation>> seleByForSta(@RequestBody Doctor doctor) {
        /**
         * @Author 李国杰
         * @Description //按日期统计查预约信息
         * @Date 10:11 2022/8/31
         * @Param [doctor]
         * @return com.woniuxy.hrp.unityhandler.ResponseEntity<java.util.List < com.woniuxy.hrp.entity.Doctor>>
         *
         **/
        Pattern s = Pattern.compile(".*" + doctor.getName() + ".*");
        int doctor_id = 0;
        if(!"".equals(doctor.getName())){
            for (Doctor d : doctorService.selectAll()) {
                if (s.matcher(d.getName()).matches()) {
                    System.out.println("匹配到的医生为"+d.getName());
                    doctor_id = d.getId();
                };
            }
        }
       System.out.println();
        List<String> date = new ArrayList<>();
        List<Integer> count = new ArrayList<>();
        for(ArrangeTime d:arrangeTimeService.getByAll()){
            System.out.println(d.getStartTime());
            date.add(d.getStartTime().substring(11));
            int num = 0;
            for (DoctorAndTime r : d.getDoctorAndTimes()) {
                DoctorAndTime doctorAndTime = doctorAndTimeService.seleById(r.getId());
                if(doctor_id!=0){
                    System.out.println(doctorAndTime.getDoctor());
                    if (doctorAndTime != null&&doctorAndTime.getDoctor().getId()==doctor_id) {
                        num += doctorAndTime.getReservations().size();
                    }
                }else {
                    if (doctorAndTime != null) {
                        num += doctorAndTime.getReservations().size();
                    }
                }
            }
            count.add(num);
        };
        List stadata = new ArrayList<>();
        stadata.add(date);
        stadata.add(count);
        return new ResponseEntity("200", "success", stadata);
    }



}
